const db = uniCloud.database();
export default {
  // 开启命名空间
  namespaced: true,

  // state 数据
  state: () => ({
    // 用户的基本信息
    runnerInfo: JSON.parse(uni.getStorageSync('runnerInfo') || '{}'),
    token: uni.getStorageSync('token') || '',
    orderList: JSON.parse(uni.getStorageSync('runnerOrder') || '[]')
  }),

  // 方法
  mutations: {
    updateRunnerInfo(state, runnerInfo) {
      state.runnerInfo = runnerInfo
      this.commit('m_runner/saveRunnerInfoToStorage')
    },
    saveRunnerInfoToStorage(state) {
      delete state.runnerInfo.password
      uni.setStorageSync('runnerInfo', JSON.stringify(state.runnerInfo))
    },
    // 更新订单列表数据
    updateOrderList(state, orderList) {
      state.orderList = orderList;
      uni.setStorageSync('runnerOrder', JSON.stringify(state.orderList))
    },
    // 更新 token 字符串
    updateToken(state, token) {
      state.token = token
      // 通过 this.commit() 方法，调用 m_user 模块下的 saveTokenToStorage 方法，将 token 字符串持久化存储到本地
      this.commit('m_runner/saveTokenToStorage')
    },
    // 将 token 字符串持久化存储到本地
    saveTokenToStorage(state) {
      uni.setStorageSync('token', state.token)
    },
    //统一管理state数据
    clearAllState(state) {
      state.runnerInfo = {};
      state.orderList = [];
      state.token = '';
    }

  },
  actions: {
    //认证成功 注册跑腿骑手个人信息
    async runnerRegister({
      commit
    }, runner) {
      const res = await db.collection('my_runner').add(runner)
      return res
    },
    //获取用户信息
    async getRunnerInfo({
      commit
    }, id) {
      const {
        result: res
      } = await db.collection('my_runner').where({
        _id: id
      }).get()
      // console.log(res.data[0]);
      commit('updateRunnerInfo', res.data[0])
      return res;
    },
    //验证手机号是否已经注册
    async hasItBeenRegistered({
      commit
    }, phone) {
      const {
        result: res
      } = await db.collection('my_runner').where({
        runnerPhone: phone
      }).get()
      const isRegistered = res.data.length > 0 ? true : false
      return {
        isRegistered,
        runnerInfo: res.data[0] || '{}'
      }
    },
    //修改骑手 头像
    async changeRunnerAvatar({
      commit
    }, runner) {
      const runnerInfo = {
        ...runner
      }
      delete runnerInfo._id
      //去除_id属性
      const res = await db.collection('my_runner').where({
        runnerPhone: runner.runnerPhone
      }).update(runnerInfo)
      commit('updateRunnerInfo', runner)
      return res
    },
    //骑手接单
    async takingOrders({
      commit
    }, order) {
      const {
        result: res
      } = await db.collection('runner_order').add(order)
      //根据订单id 修改用户订单状态为“进行中”
      const {
        result: isupdated
      } = await db.collection('my_order').where({
        _id: order.orderInfo._id
      }).update({
        state: "进行中"
      })
      console.log(isupdated);
      return res
    },
    //获取骑手所有订单
    async getAllOrderList({
      commit
    }, runnerId) {
      const {
        result: res
      } = await db.collection('runner_order').where({
        runnerId: runnerId
      }).get()
      commit('updateOrderList', res.data)
      return res
    },
    //获取骑手当前位置
    async updateAddress({
      commit
    }, {
      runnerId,
      location
    }) {
      console.log('location', location);
      const {
        result: res
      } = await db.collection('my_runner').where({
        runnerId: runnerId
      }).update({
        location: location
      })
      console.log('location', res);
      return res
    }
  },

  // 数据包装器
  getters: {}
}